package com.zhentao.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.common.Result;
import com.zhentao.domain.Area;
import com.zhentao.domain.City;
import com.zhentao.domain.Province;
import com.zhentao.service.ProvinceService;
import com.zhentao.mapper.ProvinceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 86133
* @description 针对表【province(省份表)】的数据库操作Service实现
* @createDate 2024-09-06 10:00:56
*/
@Service
public class ProvinceServiceImpl extends ServiceImpl<ProvinceMapper, Province>
    implements ProvinceService{
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Override
    public Result getProvinceList() {
        String province = stringRedisTemplate.opsForValue().get("province");
        List<Province> provinceList = JSON.parseArray(province, Province.class);
        return Result.build(provinceList, 200, "查询成功");
    }

    @Override
    public Result<String> getAddressInfo(Integer provinceId, Integer cityId, Integer areaId) {
        String province = stringRedisTemplate.opsForValue().get("province");
        List<Province> provinceList = JSON.parseArray(province, Province.class);
        String city = stringRedisTemplate.opsForValue().get("city");
        List<City> cityList = JSON.parseArray(city, City.class);
        String area = stringRedisTemplate.opsForValue().get("area");
        List<Area> areasList = JSON.parseArray(area, Area.class);
        String provinceName =provinceList.stream().filter(item -> item.getId().equals(provinceId)).findFirst().get().getName();
        String cityName =cityList.stream().filter(item -> item.getId().equals(cityId)).findFirst().get().getName();
        String areaName =areasList.stream().filter(item -> item.getId().equals(areaId)).findFirst().get().getName();
        String Address = provinceName+"市" + cityName + areaName;
        return Result.build(Address,200,"操作成功");
    }
}




